package com.goudan.rabbitmq.manager.impl;

import com.goudan.rabbitmq.config.DelayedQueueProperties;
import com.goudan.rabbitmq.manager.EventDispatcher;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("eventDispatcher")
public class EventDispatcherImpl implements EventDispatcher {

    private RabbitTemplate rabbitTemplate;

    private DelayedQueueProperties delayedQueueProperties;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Autowired
    public void setDelayedQueueProperties(DelayedQueueProperties delayedQueueProperties) {
        this.delayedQueueProperties = delayedQueueProperties;
    }

    @Override
    public void eventDispatch(Object message, Integer delay) {
        // 回调用到
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(delayedQueueProperties.getDelayedExchangeName(), delayedQueueProperties.getDelayedRoutingKey()+"666",
                message, msg -> {
                    msg.getMessageProperties().setDelay(delay);
                    return msg;
                }, correlationData);
    }
}
